package com.guguskins.back.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guguskins.back.entity.GuDisCountOrUser;
import com.guguskins.back.entity.GuDiscount;
import com.guguskins.back.entity.vo.PageVo;
import com.guguskins.back.mapper.GuDisCountOrUserMapper;
import com.guguskins.back.service.GuDisCountOrUserService;
import com.guguskins.back.service.GuDiscountService;
import com.guguskins.back.service.GuUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 用户表和折扣券关联表 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-07-14
 */
@Service
public class GuDisCountOrUserServiceImpl extends ServiceImpl<GuDisCountOrUserMapper, GuDisCountOrUser> implements GuDisCountOrUserService {
    @Autowired
    private GuDisCountOrUserMapper disCountOrUserMapper;
    @Autowired
    private GuUserService userService;
    @Autowired
    private GuDiscountService discountService;

    @Override
    public List<GuDisCountOrUser> findByList() {
        List<GuDisCountOrUser> disCountOrUsers = disCountOrUserMapper.selectList(null);
        disCountOrUsers.forEach(disCountOrUser -> {
            disCountOrUser.setUser(userService.getById(disCountOrUser.getDuUserId()));
        });
        return disCountOrUsers;
    }

    @Override
    public List<GuDisCountOrUser> findByDIdOrUserId(Integer userId, Integer duIsUse)
    {

        return disCountOrUserMapper.findByDIdOrUserId(userId, duIsUse);
    }

    @Override
    public Integer findByDisCount(Integer userId, String counponCode,BigDecimal threshold)  {
        LambdaQueryWrapper<GuDisCountOrUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GuDisCountOrUser::getDuUserId, userId);
        queryWrapper.eq(GuDisCountOrUser::getDuCounponCode, counponCode);
        GuDisCountOrUser disCountOrUser = disCountOrUserMapper.selectOne(queryWrapper);


        if (disCountOrUser == null) {
            return 0;
            //这个用户没有这个优惠券
        }
        LambdaQueryWrapper<GuDiscount> queryWrapperDisCount =new LambdaQueryWrapper<>();
        queryWrapperDisCount.eq(GuDiscount::getDcId,disCountOrUser.getDuDisCountId());
        GuDiscount   discount=discountService.getOne(queryWrapperDisCount);
        Calendar calendar = Calendar.getInstance();
        if(discount.getDcThreshold().compareTo(threshold)>=1){
            return 3;
        }

        try {
            calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(disCountOrUser.getDuBeOverdue()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
            return 1;
            //优惠券已过期
        }


        return 2;
        //该优惠券可用
    }

    @Override
    public Boolean insert(GuDisCountOrUser disCountOrUser) {
        disCountOrUser.setDuCounponCode(UUID.randomUUID().toString().replaceAll("-", ""));
        Integer no = disCountOrUserMapper.insert(disCountOrUser);
        if (no < 0) {
            return false;
        }
        return true;
    }

    @Override
    public GuDisCountOrUser findByDisCount(String counponCode) {
     GuDisCountOrUser disCountOrUser= disCountOrUserMapper.findByDisCount(counponCode);
        Calendar calendar = Calendar.getInstance();
        if(disCountOrUser==null){
            return null;
        }
        try {
            calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(disCountOrUser.getDuBeOverdue()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
            return null;
            //优惠券已过期
        }
        return disCountOrUser;
    }

    @Override
    public PageVo<GuDisCountOrUser> findBypage(int pageNum, int pageSize, String userName, String disCountName, String discountState)
    {
        if(pageNum==1){
            pageNum=0;
        }else{
            pageNum=(pageNum-1)*pageSize;
        }



        return new PageVo<>(disCountOrUserMapper.findBypage(pageNum,pageSize,userName,disCountName,discountState),disCountOrUserMapper.findByCount());
    }
}
